package com.ruoyi.system.service.impl;

import java.util.List;

import com.ruoyi.system.domain.LikedCountDTO;
import com.ruoyi.system.domain.OkrComment;
import com.ruoyi.system.domain.OkrCommentMeta;
import com.ruoyi.system.service.IOkrCommentMetaService;
import com.ruoyi.system.service.IOkrCommentService;
import com.ruoyi.system.service.IRedisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.annotation.Transient;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.OkrThumbsUpMapper;
import com.ruoyi.system.domain.OkrThumbsUp;
import com.ruoyi.system.service.IOkrThumbsUpService;
import com.ruoyi.common.core.text.Convert;
import org.springframework.transaction.annotation.Transactional;

import javax.xml.crypto.KeySelector;

/**
 * 【请填写功能名称】Service业务层处理
 * 
 * @author xiaoshuai2233@sina.com
 * @date 2020-10-16
 */
@Service
public class OkrThumbsUpServiceImpl implements IOkrThumbsUpService 
{
    @Autowired
    private OkrThumbsUpMapper okrThumbsUpMapper;

    @Autowired
    private IRedisService redisService;

    @Autowired
    private IOkrCommentService commentService;

    @Autowired
    private IOkrCommentMetaService commentMetaService;

    /**
     * 查询【请填写功能名称】
     * 
     * @param id 【请填写功能名称】ID
     * @return 【请填写功能名称】
     */
    @Override
    public OkrThumbsUp selectOkrThumbsUpById(Long id)
    {
        return okrThumbsUpMapper.selectOkrThumbsUpById(id);
    }

    /**
     * 查询【请填写功能名称】列表
     * 
     * @param okrThumbsUp 【请填写功能名称】
     * @return 【请填写功能名称】
     */
    @Override
    public List<OkrThumbsUp> selectOkrThumbsUpList(OkrThumbsUp okrThumbsUp)
    {
        return okrThumbsUpMapper.selectOkrThumbsUpList(okrThumbsUp);
    }

    /**
     * 查询【请填写功能名称】列表
     *
     * @param okrThumbsUp 【请填写功能名称】
     * @return 【请填写功能名称】
     */
    @Override
    public OkrThumbsUp selectOkrThumbsUpOne(OkrThumbsUp okrThumbsUp)
    {
        return okrThumbsUpMapper.selectOkrThumbsUpOne(okrThumbsUp);
    }



    /**
     * 新增【请填写功能名称】
     * 
     * @param okrThumbsUp 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int insertOkrThumbsUp(OkrThumbsUp okrThumbsUp)
    {
        return okrThumbsUpMapper.insertOkrThumbsUp(okrThumbsUp);
    }

    /**
     * 修改【请填写功能名称】
     * 
     * @param okrThumbsUp 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int updateOkrThumbsUp(OkrThumbsUp okrThumbsUp)
    {
        return okrThumbsUpMapper.updateOkrThumbsUp(okrThumbsUp);
    }

    /**
     * 删除【请填写功能名称】对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteOkrThumbsUpByIds(String ids)
    {
        return okrThumbsUpMapper.deleteOkrThumbsUpByIds(Convert.toStrArray(ids));
    }

    /**
     * 取消点赞，置0
     * 
     * @param id 【请填写功能名称】ID
     * @return 结果
     */
    @Override
    public int deleteOkrThumbsUpById(Long id)
    {
        return okrThumbsUpMapper.deleteOkrThumbsUpById(id);
    }

    @Override
    public int cancelOkrThumbsUp(Long userId, Integer objectId) {
        return okrThumbsUpMapper.cancelOkrThumbsUp(userId, objectId);
    }

    @Override
    @Transactional
    public void transLikedFromRedis2DB(){
        List<OkrThumbsUp> list = redisService.getLikedDataFromRedis();
        for(OkrThumbsUp thumbsUp : list){
            Integer status = thumbsUp.getStatus();
            thumbsUp.setStatus(null);
            OkrThumbsUp cur = selectOkrThumbsUpOne(thumbsUp);
            System.out.println(thumbsUp);
            if (cur == null){
                thumbsUp.setStatus(status);
                insertOkrThumbsUp(thumbsUp);
            }else{
                cur.setStatus(status);
                updateThumbsStatusFromRedis(cur);
            }
        }
    }

    @Override
    @Transactional
    public void transLikedCountFromRedis2DB() {
        List<OkrCommentMeta> list = redisService.getLikedCountFromRedis();
        for (OkrCommentMeta meta : list){
            meta.setMetaKey("thumb");
            commentMetaService.updateOkrCommentFromRedis(meta);
        }
    }

    @Override
    public int updateThumbsStatusFromRedis(OkrThumbsUp okrThumbsUp) {
        return okrThumbsUpMapper.updateThumbsStatusFromRedis(okrThumbsUp);
    }


}
